package com.xdf.testkafk.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdf.testkafk.kafka.KafkaProducer;
import com.xdf.testkafk.vo.KafkaMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author chanchaw
 * @create 2025-10-31 13:07
 */
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/kafka")
@RequiredArgsConstructor
public class KafkaController {
    private final ObjectMapper mapper;
    private final KafkaProducer producer;
    // 测试生产 kafka 消息
    @PostMapping("/produce")
    public void produceKafkaMessage(@RequestBody Map<String,String> params) throws JsonProcessingException {
        String key = params.get("key");
        String topic = params.get("topic");
        KafkaMessage kafkaMessage = new KafkaMessage(1,"Imooc-Study-Ecommerce");
        producer.sendMessage(key,mapper.writeValueAsString(kafkaMessage),topic);
    }
}
